<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>地址解析</title>

    <link href="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/layui/2.6.8/css/layui.min.css" type="text/css"
        rel="stylesheet" />

    <script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/layui/2.6.8/layui.min.js"
        type="application/javascript"></script>
    <script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/3.2.31/vue.global.min.js"
        type="application/javascript"></script>

    <style>
        .cont {
            padding-top: 30px;
        }

        .had {
            height: 70px;
            text-align: center;
            font-size: large;
            color: blueviolet;
        }
    </style>
</head>

<body>

    <div id="app" class="layui-container cont">
        <div class="layui-row">
            <div class="layui-col-md12 had">解析clash url</div>
        </div>
        <div class="layui-form ">

            <div class="layui-form-item">
                <label class="layui-form-label">输入框</label>
                <div class="layui-input-block">
                    <textarea v-model="content" placeholder="输入要解析的链接" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" @click="parseContent">提交</button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">解析结果</label>
                <div class="layui-input-block">
                    <textarea v-model="res" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item" v-show="showDownload">
                <div class="layui-input-block">
                    <button class="layui-btn" @click="downloadFile">下载内容</button>
                </div>
            </div>
        </div>
    </div>


    <script>
        const { createApp, ref } = Vue

        createApp({
            setup() {
                const content = ref('');
                const res = ref('');
                const showDownload = ref(false)

                function parseContent() {

                    res.value = ''

                    // 发送请求到后台接口
                    fetch('/parse/clash_url', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ content: content.value })
                    })
                        .then(response => {
                            if (response.status != 200) {
                                throw new Error(`status: ${response.status}`);
                            }
                            return response.json()
                        })
                        .then(data => {
                            console.log('成功调用后台接口:', data);
                            // 处理响应数据
                            if (data.code != 0) {
                                alert(`错误信息: ${data.error}`)
                            }
                            res.value = data.message
                            showDownload.value = true
                        })
                        .catch(error => {
                            console.error('调用后台接口失败:', error);
                            // 处理错误情况
                            alert(`错误信息: ${error}`)
                        });
                }

                function downloadFile() {
                    const fileName = 'config.yaml';

                    // 创建 Blob 对象
                    const blob = new Blob([res.value], { type: 'text/plain' });

                    // 创建隐藏的 <a> 元素
                    const link = document.createElement('a');
                    link.href = URL.createObjectURL(blob);
                    link.download = fileName;
                    link.style.display = 'none';

                    // 将 <a> 元素添加到 DOM 中并触发点击事件
                    document.body.appendChild(link);
                    link.click();

                    // 移除 <a> 元素并释放 Blob URL
                    document.body.removeChild(link);
                    URL.revokeObjectURL(link.href);
                }

                return {
                    content, res, showDownload, parseContent, downloadFile
                };
            }
        }).mount('#app')


    </script>
</body>

</html>